package Kruskal算法求最小生成树;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 200010;
    static int[] p = new int[N];
    static Edgs[] edgs = new Edgs[N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        for (int i = 0; i < m; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int w = in.nextInt();
            edgs[i] = new Edgs(a, b, w);
        }
        Arrays.sort(edgs, 0, m);
        for (int i = 1; i <= m; i++) {
            p[i] = i;
        }
        int res = 0, cnt = 0;
        for (int i = 0; i < m; i++) {
            int a = edgs[i].x, b = edgs[i].y, w = edgs[i].w;
            int x = find(a), y = find(b);
            if (x != y) {
                res += w;
                cnt++;
                p[x] = y;
            }
        }
        if (cnt < n - 1) {
            System.out.println("impossible");
        } else {
            System.out.println(res);
        }
    }

    private static int find(int x) {
        if (p[x] != x) {
            p[x] = find(p[x]);
        }
        return p[x];
    }
}

class Edgs implements Comparable<Edgs> {
    int x, y, w;

    public Edgs(int x, int y, int w) {
        this.x = x;
        this.y = y;
        this.w = w;
    }

    @Override
    public int compareTo(Edgs o) {
        return Integer.compare(this.w, o.w);
    }
}